import xml.etree.ElementTree as ET
import xml.sax
# import xml.dom.minidom

# tree = ET.parse('test1_All.xml')
# root = tree.getroot()
# print("tree", tree)
# print("root", len(root), len(root[0]), root[0][0][0])
# # 输出：root 4 4 <Element 'MVAC' at 0x0000023FC887A778>
# parameter = []
# print("root[0][0][0].tag.text：", root[1][2][0][0][1].tag, root[1][2][0][0][1].text)
# parameter.append(root[1][2][0][0][1].tag)
# parameter.append(root[1][2][0][0][1].text)
# #  root[0][0][0].tag.text： MVAC false
# print("parameter:", parameter)
# # 判断哪个text是True，并返回该text的tag
# child_tag = []
# child_text = []
# last_tag = []
# for child1 in root[1]:
#     # print("child", child)
#     for child2 in child1:
#         for child3 in child2:
#             if child3.tag == 'true':
#                 child_tag.append(child3.tag)
#                 child_text.append(child3.text)
#                 last_tag.append(child2.tag)
# print("child_tag:", child_tag)
# print("child_text:", child_text)
# print("last_tag:", last_tag)

unique_id = 1
# 遍历所有的节点
def walkData(root_node, level, result_list):
    global unique_id
    temp_list = [unique_id, level, root_node.tag, root_node.text]
    result_list.append(temp_list)
    unique_id += 1

    # 遍历每个子节点
    children_node = list(root_node)
    if len(children_node) == 0:
        return
    for child in children_node:
        walkData(child, level + 1, result_list)
    return

def getXmlData(file_name):
    level = 1  # 节点的深度从1开始
    result_list = []
    root = ET.parse(file_name).getroot()
    walkData(root, level, result_list)

    return result_list

def getOutdata(file_name):
    # file_name = 'C:/Users/Ben/Desktop/test1_All.xml'
    R = getXmlData(file_name)
    out_data = []
    i = -1
    for x in R:
        i = i + 1
        if x[2] == 'VerticalSpace':
            out_data.append(x)
        if x[2] == 'CDPInsulationSpaceScrewJoint':
            out_data.append(R[i + 1])
    return out_data


